#include "GetTempThread.h"
#include <QDebug>
extern QMap<QString,TcpClient*> clientList;
extern QMap<QString,SerialportClient*> serialPortClientList;

using namespace std;
GetTempThread::GetTempThread(QObject *parent): QObject{parent}
{
    this->moveToThread(&thread);
    thread.start();
}

void GetTempThread::run()
{
    logClient=loghelper::getInstance();
    if(clientList.contains(equGuid))
    {
        while (clientList[equGuid]->experimentStatus==1) {
            if(clientList[equGuid]->IsOnline()){
                if (clientList[equGuid]->tempOrder==6) {
                    QString sendInfo="62 00 00 3A 00 08 00 00 00 01 00 06 E6 6F 55 AA";
                    QByteArray finalInfo=common->GetSendData(sendInfo);

                    clientList[equGuid]->ClientSendingData(finalInfo);
                    logClient->WriteLog("CommLog","发送:\n"+sendInfo,equGuid);

                    clientList[equGuid]->tempOrder=1;
                }
                else if(clientList[equGuid]->tempOrder==8){
                    clientList[equGuid]->experimentStatus=2;
                    QSqlDatabase db = dbHelper::openConnection();
                    QSqlQuery query(db);
                    QString sql= QString("Update EquipmentInfo set EquStatus='3' where id='%1'").arg(equGuid);
                    bool res = query.exec(sql);
                    LogHelperJ::write(sql + (res?"[true]":"[false]"));

                    QString finishTime= common->GetDateTimeStr();
                    sql= QString("Update ExperimentInfo set Status='完成',FinishTime='%1' where id=(Select ExperimentGuid From EquipmentInfo Where Id='%2')").arg(finishTime,equGuid);
                    query.exec(sql);

                    sql=QString("Update StaticNumInfo Set ExpNum=ExpNum+1 Where EquGuid='%1'").arg(equGuid);
                    query.exec(sql);

                    sql=QString("Update StaticNumInfo Set TestSampleNum=TestSampleNum+(Select count(*) From SampleInfo Where ExperimentGuid=(Select ExperimentGuid From EquipmentInfo Where Id='%1') and fam<>0 and cy5<>0 and rox<>0 and vic<>0) Where EquGuid='%1'").arg(equGuid);
                    query.exec(sql);

                    sql=QString("Select RunTime From ExperimentInfo Where Id=(Select ExperimentGuid From EquipmentInfo Where Id='%1')").arg(equGuid);
                    query.exec(sql);
                    QString runTime;
                    while (query.next())
                    {
                        runTime=query.value("RunTime").toString();
                    }
                    QDateTime start = QDateTime::fromString(runTime, "yyyy-MM-ddTHH:mm:ss");
                    QDateTime end = QDateTime::fromString(finishTime, "yyyy-MM-ddTHH:mm:ss");
                    int seconds = start.secsTo(end);

                    sql=QString("Update StaticNumInfo Set TestTime=(TestTime*3600+%2)/3600.0 Where EquGuid='%1'").arg(equGuid,QString::number(seconds));
                    query.exec(sql);


                    WlkOperator *wlk=new WlkOperator();
                    wlk->SaveResultInfo(equGuid);

                    SaveExpInfo* sei = new SaveExpInfo();       // 导出报告 + 实验历史数据存储
                    sei->calculate(equGuid);
                    QMLSignal::I().emitExperimentEnd(equGuid);     // 发信号通知前端
                    thread.exit(0);
                    LogHelperJ::write("thread exit succeed");
                }
                else
                {
                    QString sendInfo="62 00 00 3A 00 0A 00 00 00 01 00 05 E7 2F 55 AA";
                    QByteArray finalInfo=common->GetSendData(sendInfo);

                    clientList[equGuid]->ClientSendingData(finalInfo);

                    logClient->WriteLog("CommLog","发送:\n"+sendInfo,equGuid);
                }
            }
            thread.sleep(2);
        }
    }
    else if(serialPortClientList.contains(equGuid))
    {
        while (serialPortClientList[equGuid]->experimentStatus==1) {
            if(serialPortClientList[equGuid]->IsOnline()){
                if (serialPortClientList[equGuid]->tempOrder==6) {
                    QString sendInfo="62 00 00 3A 00 08 00 00 00 01 00 06 E6 6F 55 AA";
                    QByteArray finalInfo=common->GetSendData(sendInfo);

                    serialPortClientList[equGuid]->ClientSendingData(finalInfo);
                    logClient->WriteLog("CommLog","发送:\n"+sendInfo,equGuid);

                    serialPortClientList[equGuid]->tempOrder=1;
                }
                else if(serialPortClientList[equGuid]->tempOrder==8){
                    serialPortClientList[equGuid]->experimentStatus=2;
                    QSqlDatabase db = dbHelper::openConnection();
                    QSqlQuery query(db);
                    QString sql= QString("Update EquipmentInfo set EquStatus='3' where id='%1'").arg(equGuid);
                    bool res = query.exec(sql);
                    LogHelperJ::write(sql + (res?"[true]":"[false]"));

                    QString finishTime= common->GetDateTimeStr();
                    sql= QString("Update ExperimentInfo set Status='完成',FinishTime='%1' where id=(Select ExperimentGuid From EquipmentInfo Where Id='%2')").arg(finishTime,equGuid);
                    query.exec(sql);

                    sql=QString("Update StaticNumInfo Set ExpNum=ExpNum+1 Where EquGuid='%1'").arg(equGuid);
                    query.exec(sql);

                    sql=QString("Update StaticNumInfo Set TestSampleNum=TestSampleNum+(Select count(*) From SampleInfo Where ExperimentGuid=(Select ExperimentGuid From EquipmentInfo Where Id='%1') and fam<>0 and cy5<>0 and rox<>0 and vic<>0) Where EquGuid='%1'").arg(equGuid);
                    query.exec(sql);

                    sql=QString("Select RunTime From ExperimentInfo Where Id=(Select ExperimentGuid From EquipmentInfo Where Id='%1')").arg(equGuid);
                    query.exec(sql);
                    QString runTime;
                    while (query.next())
                    {
                        runTime=query.value("RunTime").toString();
                    }
                    QDateTime start = QDateTime::fromString(runTime, "yyyy-MM-ddTHH:mm:ss");
                    QDateTime end = QDateTime::fromString(finishTime, "yyyy-MM-ddTHH:mm:ss");
                    int seconds = start.secsTo(end);

                    sql=QString("Update StaticNumInfo Set TestTime=(TestTime*3600+%2)/3600.0 Where EquGuid='%1'").arg(equGuid,QString::number(seconds));
                    query.exec(sql);


                    WlkOperator *wlk=new WlkOperator();
                    wlk->SaveResultInfo(equGuid);

                    SaveExpInfo* sei = new SaveExpInfo();       // 导出报告 + 实验历史数据存储
                    sei->calculate(equGuid);
                    QMLSignal::I().emitExperimentEnd(equGuid);     // 发信号通知前端
                    thread.exit(0);
                    LogHelperJ::write("thread exit succeed");
                }
                else
                {
                    QString sendInfo="62 00 00 3A 00 0A 00 00 00 01 00 05 E7 2F 55 AA";
                    QByteArray finalInfo=common->GetSendData(sendInfo);

                    serialPortClientList[equGuid]->ClientSendingData(finalInfo);

                    logClient->WriteLog("CommLog","发送:\n"+sendInfo,equGuid);
                }
            }
            thread.sleep(2);
        }
    }

    thread.exit(0);
}
